package templete_chanin;

/**
 * 定义处理模版
 */
public abstract class HandlerTemplate {
    private HandlerTemplate nextHandler;

    public void handlerRequest(int request) {
        if (canHandle(request)){
            processRequest(request);
        }else if(nextHandler != null){
            nextHandler.handlerRequest(request);
        }else {
            System.out.println("无法处理请求：" + request);
        }
    }

    //模版方法,定义处理流程
    protected abstract void processRequest(int request);

    //钩子方法,子类可以覆盖以自定义处理条件
    protected boolean canHandle(int request) {
        return false;
    }
    //设置下一个处理器
    public void setNextHandler(HandlerTemplate handlerTemplate){
        this.nextHandler=handlerTemplate;
    }

    //定义好流程执行顺序
    public  final HandlerTemplate execute(ConcreteHandlerA handlerA){
     ConcreteHandlerB handlerB=new ConcreteHandlerB();
     ConcreteHandlerC handlerC=new ConcreteHandlerC();
     handlerA.setNextHandler(handlerB);
     handlerB.setNextHandler(handlerC);
     return handlerA;
    }


}
